<template>
  <div class="container-xl bg-white">
    <CreatedQuestion
      :close="
        () => {
          router.push({ name: p.backName });
        }
      "
      :getstart="start"
      :ID="ID"
      :asynccreateorupdatequestion="p.submit"
      :istype="false"
      :isrange="false"
      :teams="teams"
      :userid="user?.ID"
    >
    </CreatedQuestion>
  </div>
</template>
<script setup lang="ts">
import { computed, onMounted } from "vue";
import CreatedQuestion from "../../components/createdquestion/index.vue";

import { useRoute, useRouter } from "vue-router";
import { useStore } from "vuex";
import { asyncgetquestion } from "../../../api/index";
import { Storekey } from "../../../interface";
import Studycodemodel from "../../../mysql_interface/studycodemodel";

// 区别修改的是  公开题目还是我的题目
const p = defineProps<{
  backName: string;
  submit: (data: Studycodemodel.Iquestion) => Promise<Studycodemodel.Iquestion>;
}>();

const router = useRouter();
const route = useRoute();
const store = useStore(Storekey);

const user = computed<Studycodemodel.Iuser>(() => {
  return store.getters.getuser;
});
const teamId = computed(() => {
  return user.value?.teamid;
});
const teams = computed(() => {
  return (
    store.state.team?.rows.filter((e) => {
      if (!e.allowquestion) return true;
      if (e.userid === user.value?.ID) return true;
      if (e.teamuser?.find((z) => z.userid === user.value?.ID && z.isAdmin))
        return true;
      return false;
    }) || []
  );
});

const ID = computed<number | undefined>(() => {
  return route.query.ID as unknown as number;
});
// XXX 临时代码
// 校验修改权限：1、只能改自己的题目。2、['manage', 'admin']的权限可以修改
async function start(data: { ID: number }) {
  const ID = data.ID;
  const q = await asyncgetquestion({ ID });
  return q;
}

onMounted(() => {});
</script>

<style scoped>
.container-xl {
  margin-bottom: 20px;
}
</style>

<style>
.row10 textarea {
  height: 80px;
}
</style>
